package com.d3works.apps.account.service.payment.invoice.payment;

import com.d3works.apps.account.db.InvoicePayment;
import com.d3works.apps.account.db.Move;
import com.d3works.apps.account.db.Reconcile;
import com.d3works.apps.account.db.repo.InvoicePaymentRepository;
import com.d3works.apps.account.db.repo.ReconcileRepository;
import com.d3works.apps.account.service.ReconcileService;
import com.d3works.apps.account.service.config.AccountConfigService;
import com.d3works.apps.account.service.move.MoveCancelService;
import com.d3works.exception.D3worksException;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;
import java.lang.invoke.MethodHandles;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InvoicePaymentCancelServiceImpl implements InvoicePaymentCancelService {

  protected AccountConfigService accountConfigService;
  protected InvoicePaymentRepository invoicePaymentRepository;
  protected MoveCancelService moveCancelService;
  protected ReconcileService reconcileService;
  protected InvoicePaymentToolService invoicePaymentToolService;

  private final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

  @Inject
  public InvoicePaymentCancelServiceImpl(
      AccountConfigService accountConfigService,
      InvoicePaymentRepository invoicePaymentRepository,
      MoveCancelService moveCancelService,
      ReconcileService reconcileService,
      InvoicePaymentToolService invoicePaymentToolService) {

    this.accountConfigService = accountConfigService;
    this.invoicePaymentRepository = invoicePaymentRepository;
    this.moveCancelService = moveCancelService;
    this.reconcileService = reconcileService;
    this.invoicePaymentToolService = invoicePaymentToolService;
  }

  /**
   * Method to cancel an invoice Payment
   *
   * <p>Cancel the eventual Move and Reconcile Compute the total amount paid on the linked invoice
   * Change the status to cancel
   *
   * @param invoicePayment An invoice payment
   * @throws D3worksException
   */
  @Transactional(rollbackOn = {Exception.class})
  public void cancel(InvoicePayment invoicePayment) throws D3worksException {

    Move paymentMove = invoicePayment.getMove();
    List<Reconcile> reconciliations = invoicePayment.getReconcileList();

    log.debug("cancel : reconcile : {}", reconciliations);

    if (!CollectionUtils.isEmpty(reconciliations)) {
      for (Reconcile reconcile : reconciliations) {
        if (reconcile.getStatusSelect() == ReconcileRepository.STATUS_CONFIRMED) {
          reconcileService.unreconcile(reconcile);
        }
      }
    }
    if (paymentMove != null
        && invoicePayment.getTypeSelect() == InvoicePaymentRepository.TYPE_PAYMENT) {
      invoicePayment.setMove(null);
      moveCancelService.cancel(paymentMove);
    } else {
      this.updateCancelStatus(invoicePayment);
    }
  }

  @Transactional(rollbackOn = {Exception.class})
  public void updateCancelStatus(InvoicePayment invoicePayment) throws D3worksException {

    invoicePayment.setStatusSelect(InvoicePaymentRepository.STATUS_CANCELED);

    invoicePaymentRepository.save(invoicePayment);

    invoicePaymentToolService.updateAmountPaid(invoicePayment.getInvoice());
  }
}
